//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------
/*
*/

package uxgenes.core;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.view.View;
import android.view.ViewGroup;



//
// Utils
//

public final class UXUtils
{
// constants //
	private static final String	TAG 		= "UXUtils";
	
	private static 		 int	DEBUG_MODE 	= -1;
	
	
	
// Debug //
	
	// CheckDebuggable
	public final static boolean CheckDebuggable (Context ctx)
	{
		DEBUG_MODE = ( 0 != ( ctx.getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE ) ) ? 1 : 0;
		return DEBUG_MODE == 1;
	}
	
	
	// IsDebuggable
	public final static boolean IsDebuggable ()
	{
		if ( DEBUG_MODE == 0 ) {
			Logger.E( TAG, "Debug mode not checked!" );
		}
		return DEBUG_MODE == 1;
	}
	
	
	
// Time //
	
	// GetTimestamp
	public final static long GetTimestamp ()
	{
		return System.currentTimeMillis();
	}
	

// Android API //
	
	// GetAPIVersion
	public final static int GetAPIVersion ()
	{
		return android.os.Build.VERSION.SDK_INT;
	}
	
	
	// CheckAPIVersion
	public final static boolean CheckAPIVersion (int api)
	{
		return GetAPIVersion() >= api;
	}
	
	
// View Utils //

	// FindViewWithType
	@SuppressWarnings("unchecked")
	public final static <T> T FindViewWithType (View parentView, Class<T> classType)
	{
		if ( parentView instanceof ViewGroup )
		{
			ViewGroup	gr = (ViewGroup) parentView;
			
			for (int i = 0; i < gr.getChildCount(); ++i)
			{
				View nextChild = gr.getChildAt(i);
				
				if ( classType.isInstance( nextChild ) ) {
					return (T) nextChild;
				}
				
				T	tmp = FindViewWithType( nextChild, classType );
				
				if ( tmp != null )
					return tmp;
			}
		}
		
		return null;
	}
	
	
// Other //
	
	
	
// Math //
	
	// Clamp
	public final static float 	Clamp (float value, float min, float max) 		{ return Math.min( Math.max( value, min ), max ); }
	public final static double 	Clamp (double value, double min, double max) 	{ return Math.min( Math.max( value, min ), max ); }
	public final static int 	Clamp (int value, int min, int max) 			{ return Math.min( Math.max( value, min ), max ); }
	public final static long	Clamp (long value, long min, long max) 			{ return Math.min( Math.max( value, min ), max ); }
	
}
